Skip to content

egl: return WGL pbuffers to the pool when DC release fails - #239

Open
RieBi wants to merge 1 commit into
google:mainfrom
RieBi:wgl-pbuffer-release-leak
Open

RieBi wants to merge 1 commit into
google:mainfrom
RieBi:wgl-pbuffer-release-leak

Conversation

@RieBi

@RieBi RieBi commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Problem

Fixes #238.

WglDisplay::releasePbuffer() returns early when wglReleasePbufferDC() fails. The pbuffer
stays in mLivePbufs. It is not reused and not destroyed. Each refill of the pool uses the live
count, so the pool doubles again and again. On NVIDIA, each pbuffer has a hidden window.

Fix

  • Always move the pbuffer to the free list, also when wglReleasePbufferDC() fails.
  • When the pool gives the pbuffer again, refreshDC() gets a new DC. If that fails, the pbuffer
    goes to mDeletePbufs. This path does not change.
  • Log the failure with GetLastError(). Log the first 8 failures, then only at powers of two.

Test

Android Emulator 37.2.13, Windows 11, NVIDIA RTX 4070 Laptop, -gpu host. 16 UI test flows,
then a framework restart.

The emulator DLL cannot be built from main: it does not export android_setOpenglesRenderer.
Thus, the test used the shipped DLL with the early return removed (2 bytes). This has the same
behavior as this change, without the log.

Boot 8 flows 16 flows After restart
NVOpenGLPbuffer windows, without change 68 170 297 293
NVOpenGLPbuffer windows, with change 43 43 43 41
GDI objects, without change 137 241 367 362
GDI objects, with change 107 112 112 109
Live pbuffers without an owner, with change 0 0 0 0

Without the change, 76 pbuffers had no owner after 8 flows. The GDI count with the change is
stable. Thus, the reuse of a pbuffer after a failed release does not leak DCs.

egl_os_api_wgl.cpp compiles with clang-cl (CMake, Windows). There are no unit tests for the
WGL back end.

Related

#200 adds m_lock to drainGlRenderThreadResources(). It can remove one cause of the failed
release. This change is necessary also with #200: any other failure has the same leak.

This change was developed with AI assistance (Claude Code; see the Assisted-by trailer).

@google-cla

google-cla Bot commented Nov 17, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

WglDisplay::releasePbuffer() returns early when wglReleasePbufferDC()
fails. At that point, releaseDC() has already set the DC as released.
The pbuffer stays in mLivePbufs. It is not reused and not destroyed.

createPbufferSurface() refills an empty pool with max(live, 8) new
pbuffers. Each stuck pbuffer increases the live count, so the pool
doubles again and again. On NVIDIA, each pbuffer has a hidden
NVOpenGLPbuffer window. Window operations on the full desktop become
slow.

The surface is not in use after release. Thus, always move the pbuffer
to the free list. When the pool gives the pbuffer again, refreshDC()
gets a new DC. If that fails, the pbuffer goes to mDeletePbufs, as
before. Log the failure, with a limit on the number of messages.

Test: Android Emulator 37.2.13, Windows 11, NVIDIA RTX 4070 Laptop,
      -gpu host, 16 UI test flows, then a framework restart. The
      emulator DLL cannot be built from main, so the test used the
      shipped DLL with the early return removed (no log).
      NVOpenGLPbuffer windows: 68 -> 297 without the change,
      43 -> 41 with it. GDI objects: 137 -> 367 without, 107 -> 109
      with. Stuck pbuffers (live, no owner) after 8 flows: 76 without,
      0 with.

Assisted-by: Claude Code:Claude-Opus-5.5
@RieBi
RieBi force-pushed the wgl-pbuffer-release-leak branch from 85ec7eb to 5c9f7fe Compare November 17, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WGL: pbuffer pool grows without limit when wglReleasePbufferDC fails

1 participant